package com.ofek.eventos;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.eventos.custom_view.EventStatus;
import com.ofek.eventos.db.EventosDatabaseService;
import com.ofek.eventos.db.datatypes.EventData;
import com.ofek.eventos.db.datatypes.helptypes.TextFieldInEvent;

public class EventListAdapter extends ArrayAdapter<EventData> {

	private final Activity context;
	private boolean edit;


	public void setEditState(final boolean edit) {
		this.edit = edit;
		notifyDataSetInvalidated();
	}

	public EventListAdapter(final Activity c, final List<EventData> list) {
		super(c, android.R.layout.simple_list_item_1, list);
		context = c;
		LocalBroadcastManager.getInstance(c).registerReceiver(
				new BroadcastReceiver() {

					@Override
					public void onReceive(final Context context,
							final Intent intent) {
						notifyDataSetChanged();
					}

				}, new IntentFilter(Constants.EVENT_NOTIFY_EVENT_SET_CHANGED));
	}
	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		View delete = null;
		final EventData event = getItem(position);
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item_event, null);
		}
		final TextView title = (TextView) convertView
				.findViewById(R.id.title);
		if (!EventosApplication.DEBUG ) 
			title.setText(((TextFieldInEvent)event.values.get((long)0)).value);
		else 
			title.setText(event.toString());
		final EventStatus unconfirmed = (EventStatus) convertView.findViewById(R.id.status);
		unconfirmed.setVisibility(edit ? View.INVISIBLE : View.VISIBLE);
		unconfirmed.setStatus(event.confirmed);
		delete = convertView.findViewById(R.id.delete);
		delete.setVisibility(edit && event.confirmed ? View.VISIBLE : View.INVISIBLE);
		final View confirm = convertView.findViewById(R.id.confirm);
		confirm.setVisibility(edit && !event.confirmed && event.uid_owner.equals(EventosApplication.EVENTOS_USER.id) ? View.VISIBLE : View.INVISIBLE);
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				confirmEvent(event);
				setEditState(false);
			}
		});
		final View unconfirm = convertView.findViewById(R.id.unconfirm);
		unconfirm.setVisibility(edit && !event.confirmed && event.uid_owner.equals(EventosApplication.EVENTOS_USER.id) ? View.VISIBLE : View.INVISIBLE);
		unconfirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				unconfirmEvent(event);
				setEditState(false);
			}
		});
		delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				deleteEvent(event);
			}
		});
		return convertView;
	}


	protected void unconfirmEvent(final EventData event) {
		final Intent intent = new Intent(EventosDatabaseService.EVENT_UNCONFIRM_REQUEST);
		intent.putExtra(EventosDatabaseService.EVENT_EXTRA, event);
		LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
		notifyDataSetChanged();
		setEditState(false);
	}

	protected void confirmEvent(final EventData event) {
		final Intent intent = new Intent(EventosDatabaseService.EVENT_CONFIRM_REQUEST);
		intent.putExtra(EventosDatabaseService.EVENT_EXTRA, event);
		LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
		notifyDataSetChanged();
		setEditState(false);
	}

	private void deleteEvent(final EventData event) {
		final Intent intent = new Intent(EventosDatabaseService.EVENT_REMOVE_REQUEST);
		intent.putExtra(EventosDatabaseService.EVENT_EXTRA, event);
		LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
		remove(event);
		notifyDataSetChanged();
	}


	public void flipEditState() {
		edit = !edit;
		notifyDataSetInvalidated();
	}

}
